package org.spring.cloud.manager.impl;


import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spring.cloud.dao.SeckillMapper;
import org.spring.cloud.manager.SeckillDetailManager;
import org.spring.cloud.manager.SeckillManager;
import org.spring.cloud.model.SeckillDetailModel;
import org.spring.cloud.model.SeckillModel;
import org.spring.cloud.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Transactional
@Component
public class SeckillManagerImpl implements SeckillManager{

	private static final Logger logger = LoggerFactory.getLogger(SeckillManagerImpl.class);
	
	@Autowired
	private SeckillMapper seckillMapper;
	
	@Autowired
	private SeckillDetailManager seckillDetailManager;

	@Override
	public void save(SeckillModel seckillModel) {
		this.seckillMapper.insert(seckillModel);
	}

	@Override
	public List<SeckillModel> querySeckilModels() {
		return this.seckillMapper.querySeckilModels();
	}

	@Override
	public SeckillModel getById(Long id) {
		return this.seckillMapper.getById(id);
	}

	@Override
	public void updateByModel(SeckillModel seckillModel) {
			this.seckillMapper.updateByModel(seckillModel);
	}

	@Override
	public Result<Boolean> secikllRush(Long userId,Long seckllId) {
		Result<Boolean> result = new Result<>(true);
		//更新活动数量
		this.seckillMapper.updateById(seckllId);
		
		//增加明细
		SeckillDetailModel detailModel = new SeckillDetailModel();
		detailModel.setSeckillId(seckllId);
		detailModel.setUserId(userId);
		detailModel.setCreateTime(new Date());
		this.seckillDetailManager.save(detailModel);
		result.setMessage("抢购成功");
		return result;
	}
	
	
}
